package com.lsk.mytuan;

import com.lsk.mytuan.entity.*;
import com.lsk.mytuan.util.Storager;

import static com.lsk.mytuan.UI.*;
import static com.lsk.mytuan.util.FloatCalc.*;

public class Main {
    private static User currentUser = null;
    private static String currentState = "guest";

    private static void guestHandler (int selection) {
        switch (selection) {
            case 1:
            {
                UI.printTitle("登陆");
                print("用户名：");
                String username = readString();
                print("密码：");
                String password = readString();
                currentUser = User.login(username, password);
                if (currentUser != null) {
                    currentState = currentUser.getRole();
                }
                break;
            }
            case 2:
            {
                UI.printTitle("注册");
                print("用户名：");
                String username = readString();
                print("密码：");
                String password = readString();
                print("角色：");
                String role = readString();
                User.register(username, password, role);
                break;
            }
            case 3:
            {
                println("感谢使用");
                System.exit(0);
            }
        }
    }

    private static void userHandler (int selection) {
        switch (selection) {
            case 1:
            {
                UI.printTitle("点餐");
                print("请输入菜品名称：");
                String name = readString();
                UI.printItemsTableHead();
                if (Item.findItems(name) == 0) {
                    System.out.println("未找到指定菜品");
                    break;
                }
                print("请输入菜品ID：");
                int itemId = readInt();
                Item item = Item.get(itemId);
                if (item == null) {
                    println("菜品不存在");
                    break;
                }
                println("请输入数量：");
                int count = readInt();
                Order order = new Order(item, count);
                currentUser.addOrder(order);
                println("点餐成功");
                break;
            }
            case 2:
            {
                UI.printTitle("查看购物车");
                UI.printOrdersTableHead();
                currentUser.showOrders("NOTPAID");
                break;
            }
            case 3:
            {
                UI.printTitle("清空购物车");
                Storager orders = currentUser.getOrders();
                for (int i = 1; i <= orders.length(); i++) {
                    Order order = (Order) orders.get(i);
                    if (order == null) {
                        continue;
                    }
                    if (order.getState().equals("NOTPAID")) {
                        orders.delete(i);
                    }
                }
                println("成功");
                break;
            }
            case 4:
            {
                UI.printTitle("支付购物车");
                Storager orders = currentUser.getOrders();
                float total = 0F;
                for (int i = 1; i <= orders.length(); i++) {
                    Order order = (Order) orders.get(i);
                    if (order == null) {
                        continue;
                    }
                    if (order.getState().equals("NOTPAID")) {
                        total = fadd(total, order.getTotal());
                    }
                }
                if (total == 0F) {
                    println("购物车为空，无需支付！");
                    break;
                }
                print("请选择支付方式（钱包/直接）:");
                boolean usingWallet = readString().equals("钱包");
                while (true) {
                    if (usingWallet) {
                        if (!currentUser.canAfford(fmul(total, 0.95F))) {
                            println("钱包余额不足，请直接支付");
                            usingWallet = false;
                        } else {
                            float back = currentUser.pay(fmul(total, 0.95F));
                            println("支付成功，钱包余额：" + back);
                            break;
                        }
                    } else {
                        print("请支付：");
                        float pay = readFloat();
                        if (pay < total) {
                            println("钱数不足，请重新支付！");
                            continue;
                        }
                        float back = fdec(pay, total);
                        println("支付成功,找零：" + back);
                        break;
                    }
                }
                for (int i = 1; i <= orders.length(); i++) {
                    Order order = (Order) orders.get(i);
                    if (order != null && order.getState().equals("NOTPAID")) {
                        order.pay();
                    }
                }
                break;
            }
            case 5:
            {
                UI.printTitle("查看订单");
                UI.printOrdersTableHead();
                System.out.println(currentUser.getOrders());
                break;
            }
            case 6:
            {
                UI.printTitle("删除订单");
                UI.printOrdersTableHead();
                Storager orders = currentUser.getOrders();
                System.out.println(orders);
                print("请输入要删除的订单ID：");
                int orderID = readInt();
                Order order = (Order) orders.get(orderID);
                if (order == null) {
                    println("订单不存在");
                    break;
                }
                if (!order.getState().equals("DONE")) {
                    println("无法删除未签收的订单");
                    break;
                }
                orders.delete(orderID);
                println("删除成功");
                break;
            }
            case 7:
            {
                UI.printTitle("支付订单");
                Storager orders = currentUser.getOrders();
                UI.printOrdersTableHead();
                System.out.println(orders);
                print("请输入要支付的订单ID:");
                int orderID = readInt();
                Order order = (Order) orders.get(orderID);
                if (order == null) {
                    println("订单不存在");
                    break;
                }
                if (!order.getState().equals("NOTPAID")) {
                    println("订单已支付");
                    break;
                }
                float total = order.getTotal();
                print("请选择支付方式（钱包/直接）:");
                boolean usingWallet = readString().equals("钱包");
                while (true) {
                    if (usingWallet) {
                        if (!currentUser.canAfford(fmul(total, 0.95F))) {
                            println("钱包余额不足，请直接支付");
                            usingWallet = false;
                        } else {
                            float back = currentUser.pay(fmul(total, 0.95F));
                            println("支付成功，钱包余额：" + back);
                            break;
                        }
                    } else {
                        print("请支付：");
                        float pay = readFloat();
                        if (pay < total) {
                            println("钱数不足，请重新支付！");
                            continue;
                        }
                        float back = fdec(pay, total);
                        println("支付成功,找零：" + back);
                        break;
                    }
                }
                order.pay();
                break;
            }
            case 8:
            {
                UI.printTitle("签收订单");
                Storager storager = currentUser.getOrders();
                UI.printOrdersTableHead();
                currentUser.showOrders("PAID");
                print("请输入要签收的订单ID：");
                int orderID = readInt();
                Order order = (Order) storager.get(orderID);
                if (order == null) {
                    println("订单不存在");
                }
                order.setState("DONE");
                println("签收成功");
                break;
            }
            case 9:
            {
                UI.printTitle("删除已签收订单");
                Storager storager = currentUser.getOrders();
                for (int i = 1; i <= storager.length(); i++) {
                    Order order = (Order) storager.get(i);
                    if (order != null && order.getState().equals("DONE")) {
                        storager.delete(i);
                    }
                }
                println("删除成功");
                break;
            }
            case 10:
            {
                printTitle("查看个人信息");
                System.out.println(currentUser);
                break;
            }
            case 11:
            {
                printTitle("更改个人信息");
                println("用户名（" + currentUser.getUsername() + "）：");
                String newUsername = readString();
                println("密码：");
                String newPassword = readString();
                currentUser.setUsername(newUsername);
                currentUser.setPassword(newPassword);
                println("更改成功");
                break;
            }
            case 12:
            {
                printTitle("钱包充值");
                print("请输入要充值的钱数：");
                float money = readFloat();
                currentUser.addMoney(money);
                println("充值成功");
                break;
            }
            case 13:
            {
                printTitle("登出");
                currentUser = null;
                currentState = "guest";
                println("成功");
                break;
            }
        }
    }

    private static void storeHandler (int selection) {
        switch (selection) {
            case 1:
            {
                UI.printTitle("增加菜品");
                print("请输入菜品名称：");
                String itemName = readString();
                print("请输入菜品价格：");
                float price = readFloat();
                int userID = currentUser.getId();
                Store store = Store.findStoreByUserId(userID);
                Item item = new Item(itemName, price, store.getId());
                store.addItem(item);
                item.add();
                println("添加成功");
                break;
            }
            case 2:
            {
                UI.printTitle("删除菜品");
                int userID = currentUser.getId();
                Store store = Store.findStoreByUserId(userID);
                Storager items = store.getItems();
                UI.printItemsTableHead();
                System.out.println(items);
                print("请输入要删除的菜品ID:");
                int itemID = readInt();
                if (items.delete(itemID) && Item.delete(itemID)) {
                    println("删除成功");
                } else {
                    println("菜品不存在");
                }
                break;
            }
            case 3:
            {
                UI.printTitle("更改菜品");
                UI.printItemsTableHead();
                Store store = Store.findStoreByUserId(currentUser.getId());
                System.out.println(store.getItems());
                print("请输入要修改的菜品ID:");
                int itemID = readInt();
                Item item = Item.get(itemID);
                print("请输入更改的菜品名（" + item.getName() + "):");
                String newName = readString();
                print("请输入更改的单价（" + item.getPrice() + "):");
                float newPrice = readFloat();
                item.setName(newName);
                item.setPrice(newPrice);
                println("修改成功");
                break;
            }
            case 4:
            {
                UI.printTitle("我的所有菜品");
                UI.printItemsTableHead();
                Store store = Store.findStoreByUserId(currentUser.getId());
                System.out.println(store.getItems());
                break;
            }
            case 5:
            {
                UI.printTitle("查看信息");
                System.out.println(Store.findStoreByUserId(currentUser.getId()));
                break;
            }
            case 6:
            {
                UI.printTitle("更改登陆密码");
                print("请输入新密码：");
                String newPassword = readString();
                currentUser.setPassword(newPassword);
                break;
            }
            case 7:
            {
                UI.printTitle("更改商家名称");
                print("请输入新名称：");
                String newName = readString();
                Store store = Store.findStoreByUserId(currentUser.getId());
                store.setName(newName);
                println("修改成功");
                break;
            }
            case 8:
            {
                printTitle("登出");
                currentUser = null;
                currentState = "guest";
                println("成功");
                break;
            }
        }
    }

    public static void main(String[] args) {
        while (true) {
            int selection = UI.welcomeInfo(currentState);
            switch (currentState) {
                case "guest":
                    guestHandler(selection);
                    break;
                case "user":
                    userHandler(selection);
                    break;
                case "store":
                    storeHandler(selection);
                    break;
            }
        }
    }
}
